// pages/addAdress/addAdress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select_address: "",
    location: "",
    adcode: "",
    addressDetails: "",
    name: "",
    phoneNumber: "",
    userId: "",
    type:"",
    details_address:"",
    people:"",
    phone:"",
    upData:"0",
    addressId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var item = JSON.parse(options.item)
    // var item = options.item;
    console.log(item)
    if(item != 1){
      var adress = item.sAddress.split(',');
      //console.log(item);
      this.setData({
        details_address: adress[1],
        addressDetails: adress[1],
        people: item.sPerson,
        phone: item.sPhone,
        name: item.sPerson,
        select_address: adress[0],
        upData: "1",
        location: item.sLocation,
        adcode:item.code,
        phoneNumber: item.sPhone,
        addressId: item.addressId
      })
    }
    try {
      const res = wx.getStorageSync("we_userInfo")
      //console.log(res)
      if (res) {
        this.setData({
          userId: res.user.userId,
        })
      }
    } catch (e) {
      console.log("获取用户id失败")
    }
  },

  getAddress: function(e) {

  },
  getAddressDetails: function(e) {
    console.log(e.detail.value)
    var that = this;
    that.setData({
      addressDetails: e.detail.value
    });
  },
  getName: function(e) {
    console.log(e.detail.value)
    var that = this;
    that.setData({
      name: e.detail.value
    });
  },
  getPhoneNumber: function(e) {
    console.log(e.detail.value)
    var that = this;
    that.setData({
      phoneNumber: e.detail.value
    });
  },
  //保存
  putAdress: function(e) {
    var id = e.currentTarget.dataset.id
    var that = this;
    if(that.data.upData != "1"){
      //新增
      if (id == 1) {
        console.log("保存")
        if (that.data.adcode != "" && that.data.addressDetails != "" && that.data.name != "" && that.data.phoneNumber != "") {
          if (that.data.phoneNumber.length != 11){
            wx.showToast({
              title: '手机号长度有问题',
              icon: 'none',
              duration: 2000,
              success: function () {
                // wx.hideToast()
              },
            })
            return false;
          }
          if (!(/^1(3|4|5|7|8|6|9)\d{9}$/.test(that.data.phoneNumber))){
            wx.showToast({
              title: '您输入的电话格式有误，请重新输入',
              icon: 'none',
              duration: 2000,
              success: function () {
                // wx.hideToast()
              },
            })
            return false;
          }
          var location = that.data.location;
          var adress = that.data.select_address + "," + that.data.addressDetails;
          wx.request({
            url: getApp().globalData.requestURL + '/api/userAddress/add',
            data: {
              code: that.data.adcode,
              userId: that.data.userId,
              sPerson: that.data.name,
              sPhone: that.data.phoneNumber,
              sAddress: adress,
              isDefault: "0",
              sLocation: location
            },
            method: 'POST',
            header: {
              "content-type": "application/json"
            },
            success: function (res) {
              console.log(res)
              if (res.data.result) {
                wx.showToast({
                  title: '添加成功',
                  icon: 'success',
                  duration: 2000,
                  mask: true,
                  success: function () {
                    setTimeout(function () {
                      wx.navigateBack();
                    }, 2500);
                  }
                })
              }
            },
            fail: function (err) {
              console.log("add adress error")
              console.log(err.errMsg)
            }

          })
        }else{
          if (that.data.addressDetails == "") {
            wx.showToast({
              title: '请输入详细信息',
              icon: 'none',
              duration: 2000,
              success: function () {
                // wx.hideToast()
              },
            })
          }
          if (that.data.name == "") {
            wx.showToast({
              title: '请输入联系人姓名',
              icon: 'none',
              duration: 2000,
              success: function () {
                // wx.hideToast()
              },
            })
          }
          if (!that.data.phoneNumber) {
            wx.showToast({
              title: '请输入联系电话',
              icon: 'none',
              duration: 2000,
              success: function () {
                // wx.hideToast()
              },
            })
          }
        }
      } else {
        console.log("删除")
      }
    }else{
      //更新
      if(id == 1){
        //保存
        if (that.data.addressDetails == "") {
          wx.showToast({
            title: '请输入详细信息',
            icon: 'none',
            duration: 2000,
            success: function () {
              // wx.hideToast()
            },
          })
        }
        if (that.data.name == "") {
          wx.showToast({
            title: '请输入联系人姓名',
            icon: 'none',
            duration: 2000,
            success: function () {
              // wx.hideToast()
            },
          })
        }
        if (!that.data.phoneNumber) {
          wx.showToast({
            title: '请输入联系电话',
            icon: 'none',
            duration: 2000,
            success: function () {
              // wx.hideToast()
            },
          })
        }
        if (that.data.adcode != "") {
          if (that.data.phoneNumber.length != 11) {
            wx.showToast({
              title: '手机号长度有问题',
              icon: 'none',
              duration: 2000,
              success: function () {
                // wx.hideToast()
              },
            })
            return false;
          }
          if (!(/^1(3|4|5|7|8|6|9)\d{9}$/.test(that.data.phoneNumber))) {
            wx.showToast({
              title: '您输入的电话格式有误，请重新输入',
              icon: 'none',
              duration: 2000,
              success: function () {
                // wx.hideToast()
              },
            })
            return false;
          }
          var location = that.data.location;
          var adress = that.data.select_address + "," + that.data.addressDetails;
          console.log(location)
          console.log(adress)
          console.log(that.data.userId)
          console.log(that.data.name)
          wx.request({
            url: getApp().globalData.requestURL + '/api/userAddress/update',
            data: {
              code: that.data.adcode,
              userId: that.data.userId,
              sPerson: that.data.name,
              sPhone: that.data.phoneNumber,
              sAddress: adress,
              isDefault: "0",
              sLocation: location,
              addressId: that.data.addressId
            },
            method: 'POST',
            header: {
              "content-type": "application/json"
            },
            success: function (res) {
              console.log(res)
              if (res.data.result) {
                wx.showToast({
                  title: '修改成功',
                  icon: 'success',
                  duration: 2000,
                  mask: true,
                  success: function () {
                    setTimeout(function () {
                      wx.navigateBack();
                    }, 2000);
                  }
                })
              }
            },
            fail: function (err) {
              console.log("updata adress error")
              console.log(err.errMsg)
            }

          })
        }
      }else{
        //删除
        if (that.data.addressDetails == "") {
          wx.showToast({
            title: '请输入详细信息',
            icon: 'none',
            duration: 2000,
            success: function () {
              // wx.hideToast()
            },
          })
        }
        if (that.data.name == "") {
          wx.showToast({
            title: '请输入联系人姓名',
            icon: 'none',
            duration: 2000,
            success: function () {
              // wx.hideToast()
            },
          })
        }
        if (!that.data.phoneNumber) {
          wx.showToast({
            title: '请输入联系电话',
            icon: 'none',
            duration: 2000,
            success: function () {
              // wx.hideToast()
            },
          })
        }
        if (that.data.adcode != "") {
          var location = that.data.location;
          var adress = that.data.select_address + "," + that.data.addressDetails;
          console.log(location)
          console.log(adress)
          console.log(that.data.userId)
          console.log(that.data.name)
          wx.request({
            url: getApp().globalData.requestURL + '/api/userAddress/update',
            data: {
              code: that.data.adcode,
              userId: that.data.userId,
              sPerson: that.data.name,
              sPhone: that.data.phoneNumber,
              sAddress: adress,
              isDefault: "0",
              sLocation: location,
              addressId: that.data.addressId,
              status:0
            },
            method: 'POST',
            header: {
              "content-type": "application/json"
            },
            success: function (res) {
              console.log(res)
              if (res.data.result) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000,
                  mask: true,
                  success: function () {
                    setTimeout(function () {
                      wx.navigateBack();
                    }, 2000);
                  }
                })
              }
            },
            fail: function (err) {
              console.log("updata adress error")
              console.log(err.errMsg)
            }

          })
        }
      }
    }
    
  },
  goMap: function(e) {
    wx.navigateTo({
      url: '../addAdressMap/addAdressMap',
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})